package org.lq.us.query;

import org.apache.commons.lang3.StringUtils;
import org.lq.us.commons.Page;
import org.lq.us.entity.Users;

import lombok.extern.log4j.Log4j;

/**
 * 用户的查询对象
 * @author linne
 *
 */
@Log4j
public class UsersQuery {
	// select * from users where name like '%%' and gender='男'  limit 0,5
	
	private String name;
	private String gender;
	private String rid;
	private String userName;
	
	/**
	 * 是否拼接limit
	 * @param bool
	 * @return
	 */
	public String toWhereSql(boolean bool,Page<Users> page) {
		StringBuilder sbf = new StringBuilder();
		/*
		 *
		 * StringUtils.isEmpty(str)
		 * null true
		 * "" true
		 * " " false
		 * "aa" false
		 * 
		 * StringUtils.isNotEmpty(str)
		 */
		//isNotBlank 判断如果不是空的返回true
		//判断只有属性中有一个有值得就拼接where
		if(StringUtils.isNotBlank(name) || StringUtils.isNotBlank(gender)
				|| StringUtils.isNotBlank(rid) || StringUtils.isNotBlank(userName)) {
			sbf.append(" where 1 = 1 ");
			if(StringUtils.isNotBlank(name)) {
				sbf.append(" and name like '%"+name+"%' ");
			}
			if(StringUtils.isNotBlank(gender)) {
				sbf.append(" and gender = '"+gender+"' ");
			}
			if(StringUtils.isNotBlank(rid)) {
				sbf.append(" and rid ="+rid);
			}
			if(StringUtils.isNotBlank(userName)) {
				sbf.append(" and userName like '%"+userName+"%' ");
			}
		}
		if(page!=null && page.getFieldName()!=null) {
			sbf.append(" order by "+page.getFieldName()+" "+page.getOrderType());
		}
		if(bool) {
			sbf.append(" limit ?,?");
		}
		log.info("user 拼接的SQL语句 : "+sbf.toString());
		return sbf.toString();
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getRid() {
		return rid;
	}
	public void setRid(String rid) {
		this.rid = rid;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	
	

}
